package lion.framework.core.task.rule;

import java.util.Date;

import lion.framework.core.task.anno.TimerRule;
import lion.dev.lang.MapJ;

import org.apache.commons.lang.StringUtils;

/**
 * a cron implements of @AbstractTimerRule <br/>
 * <b>Note:</b>not support characters <i>C</i>
 * 
 * <pre>
 * time   2013年7月22日
 * mail   hl_0074@sina.com
 * </pre>
 * 
 * @author lion
 */

@TimerRule("cron")
public class CronTimerRule extends AbstractTimerRule {

	private String expression;
	private Cron cron;

	public CronTimerRule(MapJ config) {

		super(config);

		this.expression = config.getString("cron");
		if (StringUtils.isBlank(expression)) { return; }

		this.cron = new Cron(expression);
	}

	@Override
	public boolean isExpired() {

		Date end = this.cron.getEndTime();
		if (end == null) { return false; }

		Date now = new Date();
		return now.getTime() > end.getTime();
	}

	@Override
	public boolean checkRunTime(Date last) {

		return this.cron.matches(new Date());
	}

}
